#include <iostream>
#include <thread>
#include <mutex>

std::mutex m;

// 1. 演示移动构造 / 赋值
void demo_move()
{
    std::unique_lock<std::mutex> lk1(m); // lk1 锁住 m
    std::cout << "lk1 owns lock: " << lk1.owns_lock() << '\n';

    std::unique_lock<std::mutex> lk2(std::move(lk1)); // 移动构造
    std::cout << "after move:\n";
    std::cout << "lk1 owns lock: " << lk1.owns_lock() << '\n';
    std::cout << "lk2 owns lock: " << lk2.owns_lock() << '\n';
} // lk2 析构时自动解锁

// 2. 演示 release()
void demo_release()
{
    std::unique_lock<std::mutex> lk(m); // 自动加锁
    std::cout << "lk owns lock before release: " << lk.owns_lock() << '\n';

    std::mutex *raw = lk.release(); // 释放所有权，返回裸指针：返回原始锁
    std::cout << "lk owns lock after release: " << lk.owns_lock() << '\n';
    std::cout << "raw mutex ptr is valid: " << (raw == &m) << '\n';

    raw->unlock(); // 必须手动解锁
}

int main()
{
    demo_move();
    demo_release();
    return 0;
}